package com.zzx.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zzx.common.domain.CommonResult;
import com.zzx.pojo.User;
import com.zzx.service.IUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/9/26 21:39
 */
@Tag(name = "用户控制器", description = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController
{
    @Resource
    IUserService userService;

    /**
     * getUserById
     *
     * @param userId
     * @return
     */
    @Tag(name = "用户控制器")
    @ApiOperation("通过id获取用户信息")
    @HystrixCommand(fallbackMethod = "hystrixGetUserById")
    @GetMapping("/getUserById/{userId}")
    public CommonResult getUserById(@PathVariable("userId") int userId)
    {
        // return userService.getUserById(userId);
        CommonResult userById = userService.getUserById(userId);
        Boolean success = userById.getSuccess();
        if (!success)
        {
            throw new RuntimeException("这个id=>" + userId + ",不存在该用户，或信息无法找到~");
        }
        return userById;
    }


    public CommonResult hystrixGetUserById(@PathVariable("userId") int userId)
    {
        return CommonResult.error().message("该 id=>" + userId + ",不存在该用户，或信息无法找到~");
    }

    /**
     * login
     *
     * @param user
     * @return
     */
    @Tag(name = "用户控制器")
    @ApiOperation("登录接口")
    @HystrixCommand(fallbackMethod = "hystrixUserLogin")
    @PostMapping("/login")
    public CommonResult userLogin(@RequestBody User user)
    {
        return userService.userLogin(user);
    }

    public CommonResult hystrixUserLogin(@RequestBody User user)
    {
        return CommonResult.error().message("登录失败，用户名或密码错误~");
    }

    /**
     * editUserBalance
     *
     * @param user
     * @return
     */
    @Tag(name = "用户控制器")
    @ApiOperation("编辑用户余额")
    @HystrixCommand(fallbackMethod = "hystrixEditUserBalance")
    @PutMapping("/editUserBalance")
    public CommonResult editUserBalance(@RequestBody User user)
    {
        return userService.editUserBalance(user);
    }

    public CommonResult hystrixEditUserBalance(@RequestBody User user)
    {
        return CommonResult.error().message("编辑用户余额失败，余额不能为负数");
    }
}